<template></template>

<script setup>
import axios from 'axios';
import { computed, onMounted } from 'vue';
import useCapacityStore from "@/store/capacity";
const capacityStore = useCapacityStore();

defineExpose({
  loadRecord,
  createRecord,
  deleteRecord,
  saveConfig,
})

function loadRecord() {
  axios
    .get("api/capacity_record_get.php")
    .then((res) => {
      // console.log(res.data);
      capacityStore.recordList = res.data;
    })
    .catch((err) => {
      console.log(err);
    });
}

function createRecord(item) {
  axios
    .post("api/capacity_record_save.php", item)
    .then((res) => {
      this.loadRecord();
      // console.log(res);
    })
    .catch((err) => {
      this.loadRecord();
      console.log(err);
    });
}

function updateRecord() {

}

function deleteRecord() {
  let selectedNumber = capacityStore.recordList[capacityStore.selectedRecordIndex].number;
  // console.log(selectedNumber);
  axios
    .get("api/capacity_record_delete.php?number=" + selectedNumber)
    .then(response => {
      console.log(response.data);
      this.loadRecord();
    })
    .catch(error => {
      console.error(error);
    });
}

function saveConfig({ id, idx, data }) {
  let num = capacityStore.recordList[capacityStore.selectedRecordIndex].number;
  axios
    .post('api/capacity_record_update_item.php', { num, id, idx, data }).then(response => {
      if (!response.data.result) console.info('saveConfig', response.data);
      this.loadRecord();
    }).catch(error => {
      console.error('saveConfig', error);
      this.loadRecord();
    });
}

function loadParams() {
  axios.get('api/capacity_params.php').then(response => {
    capacityStore.areaLayout = response.data.areaLayout;
    capacityStore.areaTypes = response.data.areaTypes;
    capacityStore.capacityLayout = response.data.capacityLayout;
    capacityStore.weatherOptions = response.data.weatherOptions;
  }).catch(error => { console.error('loadParams', error); });
}

onMounted(() => {
  loadParams();
});

</script>